/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.tordroid.core.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import de.tordroid.R;
import de.tordroid.core.Engine;
import de.tordroid.model.Story;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Christian Simon
 */
public class MyExpandableListAdapter extends BaseExpandableListAdapter {

    private final Context context;
    private List<Story> stories;

    public MyExpandableListAdapter(final Context con) {
        context = con;
        // init storylist
        this.stories = Engine.getCharacterStories();
        if(Engine.getCharacterStories() == null)
            this.stories = new ArrayList<Story>();     
    }

    public boolean AddGroup() {
        Engine.addCharacterStory(new Story());
        notifyDataSetChanged();
        return true;
    }
    
    public boolean AddChild(final Story story, int groupPos)
    {
        Engine.addCharacterStory(story);
        return true;
    }

    @Override
    public Story getChild(int groupPos, int childPos) {
        return stories.get(groupPos).getSubStory(childPos);
    }

    @Override
    public long getChildId(int groupPos, int childPos) {
        return 0;
    }

    @Override
    public View getChildView(int groupPos, int childPos, boolean isLastChild,
            View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.expandable_list_element, parent, false);

        // prepare UI elements
        TextView ui_titel = (TextView) rowView.findViewById(R.id.expandable_list_element_name);
        //ImageView ui_icon = (ImageView) rowView.findViewById(R.id.expandable_list_element_icon);

        Story story = getChild(groupPos, childPos);

        // manipulate UI elements
        ui_titel.setText(story.getTitel());        
        //ui_icon.setImageResource(R.drawable.ic_document);
        
        ui_titel.setTextAppearance(context, android.R.attr.textAppearanceMedium);
        return rowView;
    }

    @Override
    public int getChildrenCount(int groupPos) {
        return stories.get(groupPos).getSubStories().size();
    }

    @Override
    public Story getGroup(int groupPos) {
        return stories.get(groupPos);
    }

    @Override
    public int getGroupCount() {
        return stories.size();
    }

    @Override
    public long getGroupId(int groupPos) {
        return 0;
    }

    @Override
    public View getGroupView(int groupPos, boolean isExpanded, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.expandable_list_element, parent, false);

        // prepare UI elements
        TextView ui_titel = (TextView) rowView.findViewById(R.id.expandable_list_element_name);
        //ImageView ui_icon = (ImageView) rowView.findViewById(R.id.expandable_list_element_icon);

        Story story = stories.get(groupPos);

        // manipulate UI elements
        ui_titel.setText(story.getTitel());
        ui_titel.setTextAppearance(context, android.R.attr.textAppearanceLarge);
        //ui_icon.setImageResource(Engine.getAvailabilityIcon(character));
        return rowView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPos, int childPos) {
        return true;
    }
}
